//
//  UILabel+HBCategory.m
//  copyLabel
//
//  Created by Rambo on 2018/8/31.
//  Copyright © 2018年 LDT. All rights reserved.
//

#import "UILabel+HBCategory.h"
#import "NSObject+HBCategory.h"
#import <objc/runtime.h>


@interface UILabel ()

@property (nonatomic, strong) UILongPressGestureRecognizer *hb_LongPressGesture;

@end

@implementation UILabel (HBCategory)

+ (void)load{
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [NSObject hb_swizzlingInstanceMethodWithClass:self originSel:@selector(canBecomeFirstResponder) newSel:@selector(HBCategory_canBecomeFirstResponder)];

        [NSObject hb_swizzlingInstanceMethodWithClass:self originSel:@selector(canPerformAction:withSender:) newSel:@selector(HBCategory_canPerformAction:withSender:)];
        
    });
    
    
}

- (BOOL)HBCategory_canBecomeFirstResponder{
    if (self.hb_canCopy) {
        return YES;
    }else{
        return [self HBCategory_canBecomeFirstResponder];
    }
}

- (void)setHb_canCopy:(BOOL)hb_canCopy{
    if (hb_canCopy) {
        UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(HBCategory_longGesture:)];
        self.hb_LongPressGesture = longPressGesture;
        self.userInteractionEnabled = YES;
        [self addGestureRecognizer:longPressGesture];
    }else{
        [self removeGestureRecognizer:self.hb_LongPressGesture];
        self.hb_LongPressGesture = nil;
    }
    objc_setAssociatedObject(self, @selector(hb_canCopy), @(hb_canCopy), OBJC_ASSOCIATION_ASSIGN);
}

- (BOOL)hb_canCopy{
    return [objc_getAssociatedObject(self, @selector(hb_canCopy)) boolValue];
}


- (void)setHb_copyBlock:(UILabelCopyBlock)hb_copyBlock{
    objc_setAssociatedObject(self, @selector(hb_copyBlock), hb_copyBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (UILabelCopyBlock)hb_copyBlock{
    return objc_getAssociatedObject(self, @selector(hb_copyBlock));
}

- (UILongPressGestureRecognizer *)hb_LongPressGesture{
    return objc_getAssociatedObject(self, @selector(hb_LongPressGesture));
}

- (void)setHb_LongPressGesture:(UILongPressGestureRecognizer *)hb_LongPressGesture{
    objc_setAssociatedObject(self, @selector(hb_LongPressGesture), hb_LongPressGesture, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}



#pragma mark - Action

- (void)HBCategory_longGesture:(UILongPressGestureRecognizer*)gesture{
    if (gesture.state != UIGestureRecognizerStateBegan) return;
    if (self.hb_canCopy) {
        [self becomeFirstResponder];
        UIMenuItem *copyItem = [[UIMenuItem alloc] initWithTitle:@"复制" action:@selector(HBCategory_copyAction:)];
        
        UIMenuController *menuController = [UIMenuController sharedMenuController];
        menuController.menuItems = @[copyItem];
        [menuController setTargetRect:self.frame inView:self.superview];
        [menuController setMenuVisible:YES animated:YES];
    }
    
}

- (BOOL)HBCategory_canPerformAction:(SEL)action withSender:(id)sender{
    if (action == @selector(HBCategory_copyAction:) && self.hb_canCopy) {
        return YES;
    }
    return [self HBCategory_canPerformAction:action withSender:sender];
}

- (void)HBCategory_copyAction:(id)sender{
    
    if (self.hb_copyBlock) {
        __weak typeof(self) weakSelf = self;
        self.hb_copyBlock(weakSelf);
    }else{
        UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
        pasteboard.string = self.text;
    }

}


@end
